import time
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver
#消息列表测试用例

class ChatList:
    url=""
    driver=""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/client.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
    def ListTestByLogin(self):
        #登录状态下消息列表
        #检查元素
        #个人信息
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.user")
        #搜索框
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.search")
        #搜索按钮
        self.driver.find_element(By.CSS_SELECTOR," body > div.client - container > div > div.left > div.search > button")
        #聊天按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.tab > div.tab-session")
        #联系人列表按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.left > div.tab > div.tab-friend")
        #聊天人名称
        self.driver.find_element(By.CSS_SELECTOR,"#session-list > li:nth-child(1) > h3")
        #最近聊天内容
        self.driver.find_element(By.CSS_SELECTOR,"#session-list > li:nth-child(1) > p")
        #添加屏幕截图
        ChatDriver.getScreeShot()
        self.driver.back()


        #未登录状态下消息列表
    def ListTestByNotLogin(self):
        newurl = "http://127.0.0.1:8080/client.html"
        self.driver.get(newurl)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前用户未登录!"
        # 添加屏幕截图
        ChatDriver.getScreeShot()
        alert.accept()
        #回到登录界面
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 切换到警告框
        # 等待警告框出现
        try:
            WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
        except Exception as e:
            print(f"警告框未出现或已关闭: {e}")
        alert = self.driver.switch_to.alert
        alert.accept()
